package com.smh.a04objectStreamDemo1;

import java.io.*;
import java.util.ArrayList;

public class ObjectStreamDemo4 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Student s1 = new Student("zhangsan", 13, "nan", "hebei");
        Student s2 = new Student("lisi", 15, "nan", "hebei");
        Student s3 = new Student("wangwu", 32, "nan", "hebei");
        Student s4 = new Student("maliu", 23, "nan", "hebei");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day27-code\\file\\demo2\\b.txt"));

        ArrayList<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        oos.writeObject(list);
        oos.close();

        //读取多个自定义对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day27-code\\file\\demo2\\b.txt"));

        ArrayList<Student> list1 = (ArrayList<Student>) ois.readObject();
        list1.forEach(System.out::println);

        ois.close();


    }
}
